<template>
    <div>
		<head-top></head-top>
		<br />
		  <el-form>
			<el-row class="demo-autocomplete">
				<el-col :span="1">&nbsp;</el-col>
				<el-col :span="1.5">
					<el-button type="primary" plain><b>账户Id:</b></el-button>
				</el-col>
				<el-col :span="3">
					<el-input v-model="usercode"
							  placeholder="请输入账户Id"
							  suffix-icon="el-icon-edit-outline">
					</el-input>
				 </el-col>
				 <el-col :span="1">&nbsp;</el-col>
				 <el-col :span="1.5">
					<el-button type="primary" plain><b>角色:</b></el-button>
				 </el-col>
				 <el-col :span="3">
					<el-select v-model="roleid" filterable  placeholder="请选择">
						<el-option label="所有角色" value=""></el-option>
					    <el-option v-for="item in option1"
								   :key="item.id"
								   :label="item.rolename"
								   :value="item.id">
					    </el-option>
					 </el-select>
				  </el-col>
				  <el-col :span="1">&nbsp;</el-col>
				  <el-col :span="1.5">
					<el-button type="primary" plain><b>是否启用:</b></el-button>
				  </el-col>
				  <el-col :span="3">
					<el-select v-model="isstart">
					  <el-option label="不限" value=""></el-option>
					  <el-option label="启用" value="1"></el-option>
					  <el-option label="禁用" value="0"></el-option>
					</el-select>
				   </el-col>
				   <el-col :span="1">&nbsp;</el-col>
				   <el-col :span="2">
					   <el-button type="primary" icon="el-icon-search" @click="seacher">搜索</el-button>
				   </el-col>
			</el-row>
		</el-form>
		<el-row style="margin-top:20px ;">
			<el-col :span="1">&nbsp;</el-col>
			<el-col :span="20">
				<el-button type="primary" icon="el-icon-circle-plus"
					@click="dialogTableVisible=true">添加用户</el-button>
			</el-col>
		</el-row>
        <br />
		<el-table
		  :data="tableData"
		  height="350"
		  border
		  style="width: 100%">
		  <el-table-column
		    prop="usercode"
		    label="登录账号" align="center"
		    >
		  </el-table-column>
		  <el-table-column
		    prop="username"
		    label="用户名称" align="center"
		    >
		  </el-table-column>
		  <el-table-column
		    prop="role.rolename"
		    label="角色" align="center"
		    >
		  </el-table-column>
		  <el-table-column
		    prop="creationtime"
			:formatter="dateFormat"
		    label="创建时间" align="center"
		    >
		  </el-table-column>
		  <el-table-column
		    label="是否启用" align="center"
			>
			<template slot-scope="scope">
				<i v-if="scope.row.isstart==1" style="color: #11B95C;">启用</i>
				<i v-else style="color: red;">禁用</i>
			</template>
		  </el-table-column>
		  </el-table-column>
		  <el-table-column label="操作" align="center" width="400">
			  <template slot-scope="scope">
				<el-button
				  size="mini"
				  type="success"
				  @click="update(scope.row.id)"
				  >编辑</el-button>
				<el-button
				  size="mini"
				  type="danger"
				  @click="confirmDel(scope.row.id)"
				  >删除</el-button>
				<el-button
				  size="mini"
				  type="primary" @click="$router.push('/adminList')"
				  >预付款</el-button>
				<el-button
				  size="mini"
				  type="info" @click="$router.push('/aslogs')"
				  >LOG日志</el-button>
			  </template>
		  </el-table-column>
		</el-table>
		<el-dialog title="修改用户" width="350px" :visible.sync="dialogFormVisible">
		  <el-form :model="form" >
		    <el-form-item label="登录账号:" :label-width="formLabelWidth">
		      <el-input v-model="form.usercode" auto-complete="off"></el-input>
		    </el-form-item>
			<el-form-item label="用户名称:" :label-width="formLabelWidth">
			  <el-input v-model="form.username" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="登录密码:" :label-width="formLabelWidth">
			  <el-input type="password"  v-model="form.userpassword" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="角色:" :label-width="formLabelWidth">
			  <el-select v-model="form.roleid" filterable>
			      <el-option v-for="item in option1"
			  			   :key="item.id"
			  			   :label="item.rolename"
			  			   :value="item.id">
			      </el-option>
			   </el-select>
			</el-form-item>
		    <el-form-item label="是否启用:" :label-width="formLabelWidth">
		      <el-select v-model="form.isstart">
		        <el-option v-for="item in option2"
		        			  	:key="item.id"
		        			  	:label="item.isstart"
		        			  	:value="item.id">
		        </el-option>
		      </el-select>
		    </el-form-item>
			  <el-row>
				<el-form-item >
					<el-col :span="18">
						<el-button type="success" @click="submitForm('form')">确定</el-button>
					</el-col>
					<el-col :span="6">
						<el-button type="primary" @click="dialogFormVisible = false">取消</el-button>
					</el-col>
				</el-form-item>
			  </el-row>
		  </el-form>
		</el-dialog>
		<el-dialog title="添加用户" width="350px" :visible.sync="dialogTableVisible">
		  <el-form :model="form" >
		    <el-form-item label="登录账号:" :label-width="formLabelWidth">
		      <el-input v-model="form.usercode" auto-complete="off"></el-input>
		    </el-form-item>
			<el-form-item label="用户名称:" :label-width="formLabelWidth">
			  <el-input v-model="form.username" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="登录密码:" :label-width="formLabelWidth">
			  <el-input type="password"  v-model="form.userpassword" auto-complete="off"></el-input>
			</el-form-item>
			<el-form-item label="角色:" :label-width="formLabelWidth">
			  <el-select v-model="form.roleid" filterable>
			      <el-option v-for="item in option1"
			  			   :key="item.id"
			  			   :label="item.rolename"
			  			   :value="item.id">
			      </el-option>
			   </el-select>
			</el-form-item>
		    <el-form-item label="是否启用:" :label-width="formLabelWidth">
		      <el-select v-model="form.isstart">
		        <el-option v-for="item in option2"
		        			  	:key="item.id"
		        			  	:label="item.isstart"
		        			  	:value="item.id">
		        </el-option>
		      </el-select>
		    </el-form-item>
			  <el-row>
				<el-form-item >
					<el-col :span="18">
						<el-button type="success" @click="submitTable('form')">确定</el-button>
					</el-col>
					<el-col :span="6">
						<el-button type="primary" @click="dialogTableVisible = false">取消</el-button>
					</el-col>
				</el-form-item>
			  </el-row>
		  </el-form>
		</el-dialog>
		<div class="block"  align="center">
		    <el-pagination
		      @current-change="handleCurrentChange"
		      :current-page.sync="currentPage"
		      :page-size="pageSize"
		      layout="prev, pager, next, jumper"
		      :total="total">
		    </el-pagination>
		</div>
	</div>
</template>

<script>
	   import {mapState} from 'vuex'
	   import headTop from '../components/headTop'
	   import moment from 'moment/moment'
	   import {getUser,queryRole,getUserById,updateUser,insertUser,deleteUser} from '@/api/getData'
        export default {
			computed: {
			 	...mapState(['adminInfo']),
			 },
			components: {
				headTop,
			},
			data() {
			  return {
					option1: [],
					option2:[{id:0,isstart:'禁用'},{id:1,isstart:'启用'}],
					usercode:'',
					roleid:'',
					isstart:'',
					tableData:[],
					currentPage:1,// 当前页码
					pageSize:5,// 每页大小
				    total:0,
					dialogTableVisible: false,
					dialogFormVisible: false,
					formLabelWidth:'80px',
					form: {
						  usercode:'',
						  username: '',
						  userpassword:'',
						  isstart: '',
						  roleid:'',
						  id:'',
						},
			  };
			},
			mounted(){
				this.queryUser();
			},
			methods:{
				async queryUser(){
					let params={pageNum:this.currentPage,pageSize:this.pageSize};
					const test=await getUser(params);
					this.option1=await queryRole();
					this.tableData=test.rows;
					this.total=test.total;
					console.log(this.tableData);
				},
				dateFormat:function(row,column){
					var date = row[column.property];
			         if (date == undefined) {
			            return "";
			         }
			         return moment(date).format("YYYY-MM-DD");
				},
				async handleCurrentChange(val) {
					let params={usercode:this.usercode,roleid:this.roleid,isstart:this.isstart,pageNum:val,pageSize:this.pageSize};
					const test=await getUser(params);
					this.tableData=test.rows;
					this.total=test.total;
				},
				async seacher(){
                    this.currentPage=1;
					let params={usercode:this.usercode,roleid:this.roleid,isstart:this.isstart,pageNum:this.currentPage,pageSize:this.pageSize};
					const test=await getUser(params);
					this.tableData=test.rows;
					this.total=test.total;
				},
				async update(id){
					this.dialogFormVisible = true;
					const user=await getUserById({id:id});
					this.form.usercode=user.usercode;
					this.form.id=id;
					this.form.username=user.username;
					this.form.roleid=user.roleid;
					this.form.isstart=parseInt(user.isstart);
					this.form.userpassword=user.userpassword;
				},
				async submitForm(formName) {
					this.dialogFormVisible = false;
					let params={usercode:this.form.usercode,username:this.form.username,userpassword:this.form.userpassword,
								roleid:this.form.roleid,isstart:this.form.isstart,id:this.form.id};
					const res=await updateUser(params);
					if (res.status == 1) {
						this.$message({
							type: 'success',
							message: res.message
						});
					}else{
						this.$message({
							type: 'error',
							message: "更改失败!"
						});
					}
					/*将所有data还原为初始状态*/
					Object.assign(this.$data, this.$options.data());
					/*重新查询数据*/
					this.queryUser();
				},
				async submitTable(formName){
					let params={usercode:this.form.usercode,username:this.form.username,userpassword:this.form.userpassword,
								roleid:this.form.roleid,isstart:this.form.isstart,createdby:this.adminInfo.user.usercode};
					const res=await insertUser(params);
					if (res.status == 1) {
						this.$message({
							type: 'success',
							message: res.message
						});

					}else{
						this.$message({
							type: 'error',
							message: "插入失败!"
						});
					}
					this.dialogTableVisible = false;
					/*重新查询数据*/
					this.queryUser();
				},
				confirmDel(id){
					 this.$confirm('确定删除该用户?', '提示', {
								confirmButtonText: '确定',
								cancelButtonText: '取消',
								type: 'warning'
							  }).then(() => {
								this.deleteUser(id);
							  }).catch(() => {
								this.$message({
								  type: 'info',
								  message: '已取消删除'
								});
							 });
				},
				async deleteUser(id){
					  const res=await deleteUser({id:id});
					  if (res.status == 1) {
						this.$message({
							type: 'success',
							message: res.message
						});
					  }else{
						this.$message({
							type: 'error',
							message: "删除失败!"
						});
					}
					/*重新查询数据*/
					this.queryUser();
				}
			}
		}
</script>
